﻿#include "GLLInc.h"
#include "GLLInput.h"
#include "GLLOSApp.h"

#ifdef GLL_WIN

namespace GLL
{

	LRESULT CALLBACK TheMouseProc(int nCode, WPARAM wParam, LPARAM lParam)
	{
		return Input::Ref().MouseProc(nCode, wParam, lParam);
	}

	void Input::Create( OSApp*)
	{
		HHOOK hook;
		hook = SetWindowsHookEx(WH_MOUSE, TheMouseProc, 0, GetCurrentThreadId());
		if(!hook) GLL_Throw("input device create failed");
	}

	Input::Input()
	{
		_wheel = 0;
	}

	int Input::GetMouseWheelDelta()
	{
		int ret = _wheel;
		_wheel = 0;
		return ret;
	}

	bool Input::IsKeyDown( int vk_code )
	{
		SHORT ret = GetKeyState(vk_code);
		return (ret < 0);
	}

	LRESULT Input::MouseProc( int nCode, WPARAM wParam, LPARAM lParam )
	{
		if(nCode == HC_ACTION)
		{
			uint msg = (uint)wParam;
			PMOUSEHOOKSTRUCTEX hookStruct = (PMOUSEHOOKSTRUCTEX)lParam;
			switch(msg)
			{
			case WM_MOUSEWHEEL:
				{
					int delta = (short)(HIWORD(hookStruct->mouseData));
					_wheel += delta;
					Debug::Log2Console("wheel delta: %d\n", _wheel);
					break;
				}
			case WM_MOUSEMOVE:
				{
					_size.width = hookStruct->pt.x;
					_size.height = hookStruct->pt.y;
					break;
				}
			}
		}
		return CallNextHookEx(0, nCode, wParam, lParam);
	}

}

#endif GLL_WIN
